import { v4 as uuid } from 'uuid'
const baseURL = 'https://buxladrhdwwt.weilaios.com'
const subpath=''

/* 根据某一个字段寻找数据
 * @param {Array} list 数据
 * @param {String} field 数据中的字段
 * @param {String} value 数据值
 * @param {String} children 子数据的字段
 */
const findDataByField = (list = [], field = '', value, children = 'children') => {
  let obj = null

  for (let i = 0; i < list.length; i++) {
    if (list[i][field] === value) {
      obj = list[i]
      break
    } else if (list[i][children] && list[i][children].length) {
      obj = findDataByField(list[i][children], field, value, children)
      if (obj) break
    }
  }
  return obj
}
const regularParams = {
  'null':/^\s*$/,
  'oldNumber':/^\d*[13579]$/,
  'evenNumbers':/^\d*[02468]$/,
  'positiveInteger':/^\d+$/,
  'number':/\d+/,
  'letter':/[^a-zA-Z]/,
  'phone':/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/,
  'cardId':/(^\d{15}$)|(^\d{17}([0-9]|X)$)/,
  'email':/^\w+@[a-zA-Z0-9]{2,10}(?:\.[a-z]{2,4}){1,3}$/,
}

const DEFTAULT_ITEM_HEIGHT = 44

// 兼容pc 移动端
const HAS_TOUCH = 'ontouchstart' in window 
const START_EVENT = HAS_TOUCH ? 'touchstart' : 'mousedown'
const MOVE_EVENT = HAS_TOUCH ? 'touchmove' : 'mousemove'
const END_EVENT = HAS_TOUCH ? 'touchend' : 'mouseup'

const getClient = (e) => {
  let clientX = HAS_TOUCH ? e.changedTouches[0].clientX : e.clientX
  let clientY = HAS_TOUCH ? e.changedTouches[0].clientY : e.clientY
  return {
    x: clientX,
    y: clientY,
  }
}
const isPC = () => {
  const userAgentInfo = navigator.userAgent
  const Agents = [
    'Android',
    'iPhone',
    'SymbianOS',
    'Windows Phone',
    'iPad',
    'iPod',
  ]
  let flag = true
  for (var v = 0; v < Agents.length; v++) {
    if (userAgentInfo.indexOf(Agents[v]) > 0) {
      flag = false
      break
    }
  }
  return flag
}
const $uuid = ()=>{
  return uuid().replace(/-/g, '')
}
export {
  baseURL,
  findDataByField,
  regularParams,
  isPC,
  HAS_TOUCH,
  DEFTAULT_ITEM_HEIGHT,
  START_EVENT,
  MOVE_EVENT,
  END_EVENT,
  getClient,
   $uuid,
   subpath
}